package ex.com.jm.food.dao.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.UserDao;
import ex.com.jm.food.entity.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public User userLogin(final String userEmail,final String password) {
		// TODO Auto-generated method stub
		User user=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from User where userEmail='"+userEmail+"' and userPwd='"+password+"'";
				Query query=session.createQuery(hql);
				return query.uniqueResult();
			}
		});
		return user;
	}

	@Override
	public Object saveUser(User user) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(user);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateStatus(final Integer status,final String userId) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="update User user set user.status="+status+" where userId='"+userId+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if(count==1){
			return true;
		}
		return false;
	}

	@Override
	public User editUser(String userId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(User.class, userId);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updatePwd(final String password,final String userId) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="update User user set user.userPwd='"+password+"' where userId='"+userId+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if(count==1){
			return true;
		}
		return false;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public User checkName(final String userName) {
		
		User user=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from User where userName='"+userName+"'";
				Query query=session.createQuery(hql);
				return query.uniqueResult();
			}
		});
		return user;
		
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public User checkEmail(final String email) {
		User user=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from User where userEmail='"+email+"'";
				Query query=session.createQuery(hql);
				return query.uniqueResult();
			}
		});
		return user;
		
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public User checkPhone(final String phone) {
		User user=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from User where userPhone='"+phone+"'";
				Query query=session.createQuery(hql);
				return query.uniqueResult();
			}
		});
		return user;
		
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateAddress(final String address,final String userId) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="update User user set user.userAddress='"+address+"' where userId='"+userId+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if(count==1){
			return true;
		}
		return false;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public boolean updatePhone(final String phone,final String userId) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="update User user set user.userPhone='"+phone+"' where userId='"+userId+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if(count==1){
			return true;
		}
		return false;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateUserImage(final String image,final String userId) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="update User user set user.userImage='"+image+"' where userId='"+userId+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if(count==1){
			return true;
		}
		return false;
	}

}
